﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTH_BUSINESS;
using QLTH_DTO;
using unitlControl;

namespace QuanLy_THPT_S4.Danh_sách
{
    public partial class frmToChuyenMon : Form
    {
        private readonly clsTCMDTO tcmdto;
        private readonly clsTCMBU tcmbu;
        public frmToChuyenMon()
        {
            InitializeComponent();
            tcmdto = new clsTCMDTO();
            tcmbu = new clsTCMBU();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmToChuyenMon_Load(object sender, EventArgs e)
        {
            txtMaTCM.Enabled = false;
            gridControl1.DataSource = tcmbu.DanhSachTCM();

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tcmdto.Tentcm = txtTCM.Text;
            //if (txtMaTCM.Text == "" || txtMaTCM.Text == null)
            //{
                tcmdto.Matcm = toancuc.Tao_ma(tcmbu.macuoi(), "TCM", 2);
                tcmbu.ThemTCM(tcmdto);
                txtTCM.Text = "";
                txtTCM.Focus();
                frmToChuyenMon_Load(sender, e);

            //}
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            tcmdto.Tentcm = txtTCM.Text;
            tcmdto.Matcm = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MATCM").ToString();
            tcmbu.CapNhatTCM(tcmdto);
            //txtTCM.Text = "";
            //txtTCM.Focus();
            frmToChuyenMon_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            
            if(MessageboxUtils.YesNo("Bạn có chắc muốn xóa tổ chuyên môn này?") == DialogResult.Yes)
            {
            tcmdto.Matcm = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MATCM").ToString();
            tcmbu.XoaTCM(tcmdto);
            txtTCM.Text = "";
            txtTCM.Focus();
            frmToChuyenMon_Load(sender, e);
            }
        }

        private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            txtMaTCM.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MATCM").ToString();
            txtTCM.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TOCHUYENMON").ToString();
        }
    }
}
